package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.tradingservice.entity.ServiceComment;
import com.yuyou.fn.tradingservice.entity.ServiceOrderItem;
import com.yuyou.fn.tradingservice.mapper.ServiceCommentMapper;
import com.yuyou.fn.tradingservice.service.IServiceCommentService;
import com.yuyou.fn.tradingservice.service.IServiceOrderItemService;
import com.yuyou.fn.tradingservice.vo.AvgEvaluation;
import com.yuyou.fn.tradingservice.vo.ServiceCommentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * ServiceComment 表数据服务层接口实现类
 *
 */
@Service
public class ServiceCommentServiceImpl extends SuperServiceImpl<ServiceCommentMapper, ServiceComment,Long> implements IServiceCommentService {

    @Resource
    private IServiceOrderItemService orderItemService;

    @Override
    @Transactional
    public void addComment(ServiceCommentVo commentVo, SysUser user) {
        ServiceOrderItem orderItem = orderItemService.findById(commentVo.getOrderItemId());
        commentVo.setProductId(orderItem.getProductId());
        orderItemService.changeIsCommentsById(commentVo.getOrderItemId(),1);
        ServiceComment comment = new ServiceComment();
        BeanUtils.copyProperties(comment, commentVo);
        comment.setUserId(user.getUserId());
        comment.setUserName(user.getRealName());
        comment.setCreateTime(TimeUtils.now());
        comment.setUpdateTime(TimeUtils.now());
        comment.setImageUrl(user.getAvatarUrl());
        this.insertSelective(comment);
    }

    @Override
    public Page<ServiceComment> findPageComment(Long productId,int pageNo,int pageSize) {
        Example<ServiceComment> example = ServiceComment.createExample();
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("createTime").desc();
        criteria.andEqualTo("productId",productId);
        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public List<ServiceComment> findByProductId(Long productId) {
        Example<ServiceComment> example = ServiceComment.createExample();
        example.createCriteria().andEqualTo("productId", productId);
        return findListByExample(example);
    }

    @Override
    public AvgEvaluation findAvgByProductIdOrCategoryId(AvgEvaluation avgEvaluation) {
        return autoMapper.findAvgByProductIdOrCategoryId(avgEvaluation);
    }

    @Override
    public Long countByProductId(Long productId) {
        Example<ServiceComment> example=ServiceComment.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("productId",productId);
        return countByExample(example);
    }
}